package org.jogre.mayi.common;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import nanoxml.XMLElement;

import org.jogre.common.comm.CommTableMessage;
import org.jogre.common.games.Deck;

public class MayIDeal extends CommTableMessage {

	public static final String XML_NAME = "deal";
	
	private List<Deck> hands = null;
	private Deck deck = null;
	
	public MayIDeal(Deck deck, List<Deck> hands) {
		this.deck = deck;
		this.hands = hands;
	}
	
	public MayIDeal(XMLElement message) {

		this.hands = new ArrayList<Deck>();

		// Read child elements - card objects
		Enumeration e = message.enumerateChildren();
		
		XMLElement childMessage = (XMLElement) e.nextElement();
		if (childMessage.getName().equals(Deck.XML_NAME)) {
			this.deck = new Deck(childMessage);
						
			while (e.hasMoreElements()) {
				childMessage = (XMLElement) e.nextElement();

				if (childMessage.getName().equals(Deck.XML_NAME)) {
					Deck d = new Deck(childMessage);
					this.hands.add(d);
				}
			}
		}
		else {
			System.out.println("error with deal message");
		}
		
	}
	
	@Override
	public XMLElement flatten() {
		XMLElement message = super.flatten(XML_NAME);
		message.addChild(deck.flatten());
		for (Deck d : hands) {
			message.addChild(d.flatten());
		}
		return message;
	}

	public List<Deck> getHands() {
		return hands;
	}

	public Deck getDeck() {
		return deck;
	}
	
	
}
